package com.coursehub.course.entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 章节实体类
 * 
 * @author CourseHub Team
 */
@Entity
@Table(name = "chapters")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Chapter {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @NotBlank(message = "章节标题不能为空")
    @Size(max = 200, message = "章节标题长度不能超过200个字符")
    @Column(nullable = false, length = 200)
    private String title;
    
    @Column(columnDefinition = "TEXT")
    private String content;
    
    @NotNull(message = "章节顺序不能为空")
    @Column(name = "order_index", nullable = false)
    private Integer orderIndex;
    
    @NotNull(message = "课程ID不能为空")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "course_id", nullable = false)
    private Course course;
    
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private ChapterType type = ChapterType.TEXT;
    
    @Column(name = "video_url")
    private String videoUrl;
    
    @Column(name = "material_url")
    private String materialUrl;
    
    @Column(name = "duration_minutes")
    private Integer durationMinutes;
    
    @Column(name = "is_free")
    private Boolean isFree = true;
    
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;
    
    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
        updatedAt = LocalDateTime.now();
    }
    
    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }
    
    /**
     * 章节类型枚举
     */
    public enum ChapterType {
        TEXT("文本"),
        VIDEO("视频"),
        DOCUMENT("文档"),
        EXERCISE("练习"),
        QUIZ("测验");
        
        private final String description;
        
        ChapterType(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
}